package com.yonyougov.devops.cruxcisupport.harbor.artifact;

import com.yonyougov.devops.cruxcisupport.harbor.artifact.Dto.DelHarborArtifactDto;
import com.yonyougov.devops.cruxcisupport.harbor.artifact.Dto.ReferencyAndVersion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/api/v2/harbor")
public class HarborArtifactController {
    @Autowired
    private HarborArtifactService harborArtifactService;

    @GetMapping(value = "/artifact")
    public HarborArtifactsPage getArtifacts(@RequestParam String projectName,@RequestParam String repositoryName
            ,@RequestParam Integer page,@RequestParam Integer pageSize,@RequestParam(required = false) String tag,@RequestParam(required = false) String sort,@RequestParam(required = false) String order,@RequestParam(required = false) String startDate,@RequestParam(required = false) String endDate){

        Integer totalCount = harborArtifactService.getArtifactRepositoryTotal(projectName, repositoryName);;
        if(StringUtils.isNotBlank(tag)){
            //模糊查询获取总数量
            List<HarborArtifacts> artifacts = harborArtifactService.getArtifacts(projectName, repositoryName, tag, 1, totalCount);
            totalCount = artifacts.size();
        }
        if(StringUtils.isBlank(sort) && StringUtils.isBlank(endDate)){
            List<HarborArtifacts> artifacts = harborArtifactService.getArtifacts(projectName, repositoryName, tag, page, pageSize);
            return HarborArtifactsPage.builder().data(artifacts).totalCount(totalCount).build();
        }else {
            List<HarborArtifacts> artifactsAll = harborArtifactService.getAllArtifact(projectName, repositoryName, tag,page, pageSize,order,sort,startDate,endDate);
            totalCount= artifactsAll.size();
            if (page == 1) {
                if ((page * pageSize) <= artifactsAll.size()) {
                    artifactsAll= artifactsAll.subList(0, (page * pageSize));
                } else {
                    artifactsAll= artifactsAll.subList(0, artifactsAll.size());
                }
            } else {
                if ((page * pageSize) <= artifactsAll.size()) {
                    artifactsAll= artifactsAll.subList(((page - 1) * pageSize) + 1, (page * pageSize));
                } else {
                    artifactsAll= artifactsAll.subList(((page - 1) * pageSize) + 1, artifactsAll.size());
                }
            }
            return HarborArtifactsPage.builder().data(artifactsAll).totalCount(totalCount).build();
        }
    }


    @GetMapping(value = "/artifact/{project}/{repository}/total")
    public Integer getArtifactRepositoryTotal(@PathVariable String project,@PathVariable String repository){
        return harborArtifactService.getArtifactRepositoryTotal(project,repository);
    }

    @DeleteMapping(value = "/artifact/{project}/{repository}/{reference}/{tags}")
    public void deleteArtifact(@PathVariable String reference,@PathVariable String project,@PathVariable String repository,@PathVariable String tags){
        harborArtifactService.deleteArtifact(project,repository, ReferencyAndVersion.builder().reference(reference).version(tags).build());
    }

    @PostMapping(value = "/artifact/batch/del")
    public void batchDelArtifacts(@RequestBody @Valid List<DelHarborArtifactDto> delHarborArtifactDtoList){
        harborArtifactService.batchDelArtifacts(delHarborArtifactDtoList);
    }

    @GetMapping(value = "/artifact/image")
    public boolean findAppVersionfromHaber(@RequestParam String app,@RequestParam String version){
        return harborArtifactService.findAppVersionfromHaber(app,version);
    }
}
